<?php
##############################################
#
#   商品
# @ Time : 2017-1-25 - 下午10:04:52
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Model;

class GoodsModel extends IndexBaseModel{
	/*商品表  */
	protected $tableName = 'goods';
	public $cids = array();	//检索的cid
	public $lids = array();	//检索的lid
	public $price = '';		//价格检索条件
	public $order = '';		//排序
	
	/**
	 * 获取商品
	 */
	public function getGoods(){
		if(!empty($this -> cids)){
			$map['cid'] = array('in', $this -> cids);
		}
		if(!empty($this -> lids)){
			$map['lid'] = array('in', $this -> lids);
		}
		if(!empty($this -> price)){
			$map['price'] = $this -> price;
		}
		$map['end_time'] = array('gt', time());
		
		$fields = array(
				'gid',
				'goods_img',
				'main_title',
				'sub_title',
				'price',
				'old_price',
				'buy'
		);
		
		$count = $this -> where($map) -> count();// 查询满足要求的总记录数
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page -> show();
		
		$result = $this -> field($fields) -> where($map) ->order($this -> order) ->limit($Page->firstRow.','.$Page->listRows) -> select();
		return array($result, $show);
	}
	
	/**
	 * 获取商品详情页数据
	 * @param int $gid
	 */
	public function getGoodsDetail($gid){
		$model = M();
		$sql = "select g.*, s.shop_name, s.shop_address, s.shop_tel,
				l.lname, c.cname, d.detail 
				from mt_goods as `g` LEFT JOIN  mt_shop as `s` ON g.shopid = s.shopid
		 		LEFT JOIN mt_locality as `l` ON g.lid = l.lid 
				LEFT JOIN mt_category as `c` ON g.cid = c.cid
				LEFT JOIN mt_goods_detail as `d` ON g.gid = d.goods_id where gid = $gid";
		return $model -> query($sql);
		
	}
	
}
